package cn.tedu.ivos.dictOption.service.Impl;

import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dictOption.mapper.DictOptionMapper;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionSaveParam;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictOption.pojo.entity.DictOption;
import cn.tedu.ivos.dictOption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictOption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictOptionServiceImpl implements DictOptionService {
    @Autowired
    private DictOptionMapper dictOptionMapper;
    @Autowired
    private DictMapper dictMapper;
    @Override
    public List<DictOptionVO> selectDictOption(DictOptionQuery dictOptionQuery) {
        log.debug("开始处理【查询字典项】的请求，参数：{}",dictOptionQuery);
        List<DictOptionVO> dictOptionVOList = dictOptionMapper.selectDictOption(dictOptionQuery);
        return dictOptionVOList;
    }

    @Override
    public int saveDictOption(DictOptionSaveParam dictOptionSaveParam) {
        log.debug("开始处理【保存字典项】的请求，参数：{}", dictOptionSaveParam);
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionSaveParam,dictOption);
        if(dictOption.getId()==null){
            dictOption.setCreateTime(new Date());
            return dictOptionMapper.insertDictOption(dictOption);
        }else {
            dictOption.setUpdateTime(new Date());
            return dictOptionMapper.updateDict(dictOption);
        }
    }

    @Override
    public int deleteDictOption(Long id) {
        log.debug("开始处理【删除字典项】的请求，参数：{}", id);

        return dictOptionMapper.deleteDictOptionById(id);
    }

    @Override
    public List<DictOptionVO> selectDictOptionByCode(String code) {
        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> dictVOList = dictMapper.selectDict(dictQuery);
        if(dictVOList!=null&&dictVOList.size()>0){
            Long dictId = dictVOList.get(0).getId();
            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setDictId(dictId);
            List<DictOptionVO> dictOptionVOList = dictOptionMapper.selectDictOption(dictOptionQuery);
            log.debug("根据字典编码查询字典项，结果为：{}", dictOptionVOList);
            return dictOptionVOList;
        }
        return null;
    }
}
